﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using Data.Common;
using Data.Model;
using DataAccess.BikeManage;
using Ninject;
using WinForm.Helper;

namespace WinForm.CarManage
{
    public partial class FrmParkingAnalysis : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly ILockArea _lockArea = Kernel.Get<ILockArea>();
        private readonly IControlBox _controlBox = Kernel.Get<IControlBox>();

        public FrmParkingAnalysis()
        {
            InitializeComponent();
        }

        private void SetChartValue(int areaId)
        {
            try
            {
                IList<jsh_controlbox> boxes = new List<jsh_controlbox>();
                boxes = _controlBox.GetControlBox(areaId).Obj;
                if (boxes.Count > 0)
                {
                    chtParking.ChartAreas["chtArea1"].Visible = true;
                    //IList<jsh_controlbox> boxesASC = boxes.OrderBy(b => b.bikecount).ToList();

                    IList<jsh_controlbox> boxesASC = boxes.ToList();

                    chtParking.Series["series1"].Points.
                        DataBindXY(boxesASC.Select(b => b.Address).ToList(), boxesASC.Select(b => b.Bikenum).ToList());
                    chtParking.Series["series2"].Points.
                        DataBindXY(boxesASC.Select(b => b.Address).ToList(), boxesASC.Select(b => b.Pilenum).ToList());


                }
                else
                {
                    chtParking.ChartAreas["chtArea1"].Visible = false;


                }
            }
            catch (Exception e)
            {

            }


        }
        private void FrmParkingAnalysis_Load(object sender, EventArgs e)
        {
            try
            {
                //停车区域选择框
                cmbParkingArea.DataSource = _lockArea.GetLockArea(0).Obj;
                cmbParkingArea.DisplayMember = "Area";
                cmbParkingArea.ValueMember = "id";



                chtParking.ChartAreas.Add("chtArea1");
                chtParking.Series.Add("series1");
                chtParking.Series.Add("series2");
                chtParking.Legends.Add("legends1");

                chtParking.Legends["legends1"].DockedToChartArea = "chtArea1";

                chtParking.Series["series1"].LegendText = "停车数量";
                chtParking.Series["series1"].IsValueShownAsLabel = true;
                chtParking.Series["series1"].Legend = "legends1";
                chtParking.Series["series2"].IsValueShownAsLabel = true;
                chtParking.Series["series2"].LegendText = "锁柱数量";

                //设定chart
                Title title = new Title();
                title.Text = "停车点停车数量图";
                chtParking.Titles.Add(title);

                //chartarea
                chtParking.ChartAreas["chtArea1"].Area3DStyle.Enable3D = true;
                chtParking.ChartAreas["chtArea1"].Area3DStyle.IsClustered = true;
                chtParking.ChartAreas["chtArea1"].Area3DStyle.Rotation = 40;
                chtParking.ChartAreas["chtArea1"].Area3DStyle.Inclination = 50;
                chtParking.ChartAreas["chtArea1"].Area3DStyle.PointDepth = 30;
                chtParking.ChartAreas["chtArea1"].Area3DStyle.WallWidth = 0;
                chtParking.ChartAreas["chtArea1"].Area3DStyle.LightStyle = LightStyle.Realistic;
                chtParking.ChartAreas["chtArea1"].BackColor = Color.FromArgb(240, 240, 240);

                //图形设置
                int areaid = Converts.ConvertToInt(cmbParkingArea.SelectedValue == null ? "" : cmbParkingArea.SelectedValue.ToString());
                SetChartValue(areaid);
            }
            catch (Exception)
            {

            }

        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            int areaid = Converts.ConvertToInt(cmbParkingArea.SelectedValue == null ? "" : cmbParkingArea.SelectedValue.ToString());
            SetChartValue(areaid);
        }
    }
}
